<script lang="ts" setup name="SmartMediaProjectManager">
import * as ProjectManagerApi from '@/api/creative/smart-media/production'
import { ContentWrap } from '@/components/ContentWrap'
import CreateProjectDialog from './template-manager/create-project-dialog.vue'
import { formatDate } from '@/utils/formatTime'

const message = useMessage()
const { t } = useI18n()

const router = useRouter()

const loading = ref(false)
const total = ref(0)
const list = ref<ProjectManagerApi.EditingProject[]>([])
const queryParams = reactive<ProjectManagerApi.SearchEditingProjectRequest>({
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref()

const getList = async () => {
  loading.value = true
  try {
    const data = await ProjectManagerApi.searchEditingProject(queryParams)
    console.log('🚀 ~ file: project-manager.vue:24 ~ getList ~ data:', data)
    list.value = data.projectList
    total.value = data.totalCount
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleEdit = (project: { projectId: string; templateType: string }) => {
  if (project.templateType === 'VETemplate')
    router.push(`/creative/media/advanced-template-editor?projectId=${project.projectId}`)
  else router.push(`/creative/media/editor?projectId=${project.projectId}`)
}

const handleDelete = async (projectId: string) => {
  try {
    await message.delConfirm()
    await ProjectManagerApi.deleteEditingProject({
      projectId
    })
    message.success(t('common.delSuccess'))
    getList()
  } catch {}
}

const createProjectDialogRef = ref()
const openCreateProjectDialog = () => {
  createProjectDialogRef.value?.open()
}

const statusMap = {
  Draft: '草稿',
  Producing: '合成中',
  Produced: '合成成功',
  ProduceFailed: '合成失败'
}
const templateTypeMap = {
  None: '无模板',
  Timeline: '普通模板',
  VETemplate: '高级模板'
}

onMounted(() => {
  getList()
})
</script>

<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="项目状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择项目状态"
          clearable
          class="!w-240px"
        >
          <el-option label="全部" value="" />
          <el-option v-for="key in statusMap" :key="key" :label="statusMap[key]" :value="key" />
        </el-select>
      </el-form-item>
      <!-- 按钮 -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="primary" plain @click="openCreateProjectDialog">
          <Icon icon="ep:plus" class="mr-5px" /> 新的创作
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <!-- <el-table-column label="id" align="center" prop="projectId" /> -->
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="封面" align="center" prop="coverUrl" />
      <el-table-column label="类型" align="center">
        <template #default="scope">
          <span>{{ templateTypeMap[scope.row.templateType] || '未知类型' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template #default="scope">
          <span>{{ formatDate(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center">
        <template #default="scope">
          <span>{{ formatDate(scope.row.modifiedTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="时长" align="center" prop="duration" /> -->
      <el-table-column label="状态" align="center">
        <template #default="scope">
          <span>{{ statusMap[scope.row.status] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleEdit(scope.row)"
            v-hasPermi="['creative:smart-media:project']"
          >
            剪辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.projectId)"
            v-hasPermi="['creative:smart-media:project']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <CreateProjectDialog ref="createProjectDialogRef" />
</template>
